package com.flexrrb.controls.tooltips
{
	import com.flexrrb.core.IRRBTooltip;
	import com.flexrrb.skin.YellowTooltipSkin;
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	import mx.controls.Label;
	import mx.core.UIComponent;
	import mx.effects.IAbstractEffect;
	import mx.effects.IEffect;
	import mx.skins.ProgrammaticSkin;
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;
	
	public class SimpleTooltip extends UIComponent implements IRRBTooltip
	{
		protected var _textField:Label;
		protected var _target:UIComponent;
		
		protected var _text:String;
		protected var _invalidateText:Boolean;
		protected var _offset:Point;
		
		protected var _backgroundSkin:ProgrammaticSkin;
		protected var _showEffect:IEffect;
		protected var _hideEffect:IEffect;
		
		private static var _classConstructed:Boolean = constructClass();
		
		public function SimpleTooltip()
		{
			super();
			offset = new Point(-10, 2);
		}
		
		private static function constructClass():Boolean
		{
			var selector:CSSStyleDeclaration = StyleManager.getStyleDeclaration("SimpleTooltip")
			
			if(!selector)
			{
				selector = new CSSStyleDeclaration();	
			}
			
			selector.defaultFactory = function():void
			{
				this.backgroundSkin = YellowTooltipSkin;
				//this.backgroundColor = 0xf9fb7b;
			}
			
			StyleManager.setStyleDeclaration("SimpleTooltip", selector, false);
			
			return true;
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			_textField = new Label();
			
			var backgroundSkinClass:Class = getStyle("backgroundSkin") as Class;
			_backgroundSkin = new backgroundSkinClass();
			
			if(_backgroundSkin)
			{
				addChild(DisplayObject(_backgroundSkin));
			}
			
			addChild(_textField);
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			_textField.setActualSize(_textField.getExplicitOrMeasuredWidth(), _textField.getExplicitOrMeasuredHeight());
		}
		
		override protected function measure():void
		{
			super.measure();
			
			measuredWidth = _textField.getExplicitOrMeasuredWidth();
			measuredHeight = _textField.getExplicitOrMeasuredHeight();
			
			if(_backgroundSkin)
			{
				_backgroundSkin.setActualSize(measuredWidth, measuredHeight);
			}
			
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if(_invalidateText)
			{
				_textField.text = _text;
				_invalidateText = false;
			}
		}
		
		public function get showEffect():IAbstractEffect
		{
			return _showEffect;
		}
		
		public function get hideEffect():IAbstractEffect
		{
			return _hideEffect;
		}
		
		public function setupPosition():void
		{
			var point:Point = new Point(target.x + target.width + offset.x, target.y + target.height + offset.y);
			point = target.localToGlobal(point);
			
			this.x = point.x;
			this.y = point.y;		
		}
		
		public function get target():UIComponent
		{
			return _target;
		}
		
		public function set target(value:UIComponent):void
		{
			_target = value;
			text = _target.toolTip;
		}
		
		public function get text():String
		{
			return _text;
		}
		
		public function set text(value:String):void
		{
			_text = value;
			_invalidateText = true;
			invalidateProperties();
			invalidateSize();
		}
		
		public function get offset():Point
		{
			return _offset;
		}
		
		public function set offset(value:Point):void
		{
			_offset = value;
		}
	}
}